package org.eclipse.jenkins.view;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;

import dk.contix.eclipse.hudson.views.RefreshAction;

public class JenkinsView extends ViewPart implements IPartListener {
	private RefreshAction refreshAction;
	private IToolBarManager manager;
	private Tree tree;
	private TreeViewer treeViewer;
	
	public JenkinsView() {
		setTitleImage(ResourceManager.getPluginImage("org.eclipse.jenkins", "icons/jenkins.png"));
		setTitleToolTip("Jenkins Monitoring");
		setPartName("Jenkins");
	}
	
	private class TableLabelProvider extends LabelProvider implements ITreePathLabelProvider {

		@Override
		public void updateLabel(ViewerLabel label, TreePath elementPath) {

			label.setText("" + elementPath.getLastSegment());
			
			
		}
	}
	
	private static class ContentProvider implements ITreeContentProvider {
		
		@Override
		public Object[] getElements(Object inputElement) {
			return new Object[]{"SSS", "DDD", "EEE"};
		}
		
		@Override
		public void dispose() {
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
		
		@Override
		public Object[] getChildren(Object parentElement) {
			// TODO Auto-generated method stub
			return null;
		}
		@Override
		public Object getParent(Object element) {
			// TODO Auto-generated method stub
			return null;
		}
		@Override
		public boolean hasChildren(Object element) {
			
			return element.equals("DDD");
			
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		
		getSite().getPage().addPartListener(this);

		treeViewer = new TreeViewer(parent);

		
		tree = treeViewer.getTree();
		
		tree.setLinesVisible(true);
		tree.setHeaderVisible(true);
		
		TreeColumn enabledColumn = new TreeColumn(tree, SWT.NONE);
		enabledColumn.setText("Monitored");
		
		TreeColumn statuscolumn = new TreeColumn(tree, SWT.LEAD);
		statuscolumn.setText("Status");
		
		TreeColumn weatherColumn = new TreeColumn(tree, SWT.LEAD);
		weatherColumn.setText("Weather");
		
		TreeColumn projectColumn = new TreeColumn(tree, SWT.NONE);
		projectColumn.setText("Name");
		
		TreeColumn lastSuccessColumn = new TreeColumn(tree, SWT.LEAD);
		lastSuccessColumn.setText("Last Success");
		
		TreeColumn lastFailureColumn = new TreeColumn(tree, SWT.LEAD);
		lastFailureColumn.setText("Last Failure");
		
		TreeColumn lastStableColumn = new TreeColumn(tree, SWT.LEAD);
		lastStableColumn.setText("Last Stable");
		
		TreeColumn lastDurationColumn = new TreeColumn(tree, SWT.LEAD);
		lastDurationColumn.setText("Last Duration");
		
		TreeColumn lastBuilderColumn = new TreeColumn(tree, SWT.LEAD);
		lastBuilderColumn.setText("Last Builder");

		
		TreeColumn[] columns = tree.getColumns();
		
		for (TreeColumn column : columns) {
			
			column.setMoveable(true);
			column.pack();
			
		}
		
		treeViewer.setLabelProvider(new TableLabelProvider());
		treeViewer.setContentProvider(new ContentProvider());
		treeViewer.setInput(getViewSite());
		
		IActionBars bars = getViewSite().getActionBars();
		
		manager = bars.getToolBarManager();
		refreshAction = new RefreshAction(null);
		refreshAction.setToolTipText("Refresh status for all projects");
		
		ImageDescriptor refreshImage = ImageDescriptor.createFromImage(ResourceManager.getPluginImage("org.eclipse.jenkins", "icons/refresh.png"));
		
		refreshAction.setImageDescriptor(refreshImage);
		manager.add(refreshAction);

	}

	@Override
	public void setFocus() {
		
	}
	
	@Override
	public void dispose() {
		
		getSite().getPage().removePartListener(this);
		
		super.dispose();
	}

	@Override
	public void partActivated(IWorkbenchPart part) {
		System.out.println("partActivated");
		
	}

	@Override
	public void partBroughtToTop(IWorkbenchPart part) {
		System.out.println("partBroughtToTop");
		
	}

	@Override
	public void partClosed(IWorkbenchPart part) {
		System.out.println("partClosed: " + part.getTitle());
		
	}

	@Override
	public void partDeactivated(IWorkbenchPart part) {
		System.out.println("partDeactivated");
		
	}

	@Override
	public void partOpened(IWorkbenchPart part) {
		System.out.println("partOpened");
		
	}

	private void fillContextMenu(IMenuManager manager) {
//		manager.add(action1);
//		manager.add(action2);
		manager.add(new Separator());
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}

}
